//
//  FeedbackViewController.m
//  阿帕
//
//  Created by mac on 2020/1/2.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "FeedbackViewController.h"
#import "CYPlaceholderTextView.h"
#import "XMTextView.h"
#import "TXPingTaiRedCell.h"
#import "ZTXMeRedCell.h"
#import "ZTXRedBottomCell.h"
#import "ZTXChuLiHeaderCell.h"

@interface FeedbackViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSString *code;
@property (nonatomic, strong) NSString *orderDetailCode;
@property (nonatomic, strong) NSString *parentCode;
@property (nonatomic, strong) NSString *contentStr;
@property (nonatomic, strong) NSString *orderCode;
@property (nonatomic, strong) NSMutableDictionary *headerdic;
@property (nonatomic, strong) NSString *headerContentStr;


@end

@implementation FeedbackViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"投诉意见反馈";
    [self confirmButtonClick];
    self.dataArray = [@[] mutableCopy];
    self.headerdic = [NSMutableDictionary dictionary];
    [self.view addSubview:self.tableView];
}

- (void)confirmButtonClick {
    
    WEAKSELF
    [self.dataArray removeAllObjects];
    NSString *urlStr = CJStringWithFormat(@"%@%@/%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], FEEDBACK,[UserDefaultUtil valueForKey:DriverPartyCode]);
    [HTTPManager getNetworkRequest:urlStr withParameter:@{} withVC:self netName:@"投诉意见反馈" success:^(id response, NSString *msg) {
        NSDictionary *dict = response[0];
        [weakSelf.dataArray addObjectsFromArray:dict[@"childRenVoList"]];
        weakSelf.code = dict[@"code"];
        weakSelf.parentCode = dict[@"parentCode"];
        weakSelf.headerContentStr = dict[@"content"];
        [weakSelf.headerdic setValue:dict[@"messager"] forKey:@"handler"];
        [weakSelf.headerdic setValue:dict[@"gmtCreated"] forKey:@"gmtCreated"];
        [weakSelf.headerdic setValue:dict[@"content"] forKey:@"handleIdea"];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView scrollToRow:0 inSection:2 atScrollPosition:(UITableViewScrollPositionBottom) animated:NO];

    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}


#pragma mark - LazyLoad
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, 10, ScreenW, ScreenH - NavHeight - 10);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        //注册单元格
        [self.tableView registerNib:[UINib nibWithNibName:@"ZTXChuLiHeaderCell" bundle:nil] forCellReuseIdentifier:@"ZTXChuLiHeaderCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"TXPingTaiRedCell" bundle:nil] forCellReuseIdentifier:@"TXPingTaiRedCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"ZTXMeRedCell" bundle:nil] forCellReuseIdentifier:@"ZTXMeRedCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"ZTXRedBottomCell" bundle:nil] forCellReuseIdentifier:@"ZTXRedBottomCell"];
        
    }
    
    return _tableView;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        if (self.headerContentStr.length == 0 || ![self.headerContentStr isNotBlank]) {
            return 0;
        }else{
            return 1;
        }
        
    }else if (section == 1){
        return self.dataArray.count;
    }else{
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ZTXMeRedCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ZTXMeRedCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell fuzhiFanKZTXMeRedCell:self.headerdic];
        return cell;
    }else if (indexPath.section == 1){
        NSDictionary *dic = self.dataArray[indexPath.row];
        if ([CJStringWithFormat(@"%@",dic[@"partyType"]) isEqualToString:@"PLAT_ADMIN"]) {
            TXPingTaiRedCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"TXPingTaiRedCell" forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell fuzhiFanKZTXMeRedCell:dic];
            return cell;
        }else{
            ZTXMeRedCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ZTXMeRedCell" forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell fuzhiFanKZTXMeRedCell:dic];
            return cell;
        }
    }else{
        ZTXRedBottomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ZTXRedBottomCell" forIndexPath:indexPath];
        [cell.huifuButton addTarget:self action:@selector(huifuButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        cell.txtView.textViewListening = ^(NSString *textViewStr) {
            self.contentStr = textViewStr;
        };
        cell.txtView.placeholder = @"请填写产品意见，我们将不断优化体验";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
   
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return [ZTXMeRedCell fuzhiDicHeight:self.headerdic];
    }else if (indexPath.section == 1){
        NSDictionary *dic = self.dataArray[indexPath.row];
        if ([CJStringWithFormat(@"%@",dic[@"partyType"]) isEqualToString:@"PLAT_ADMIN"]) {
            return [TXPingTaiRedCell fuzhiDicHeight:dic];
        }else{
            return [ZTXMeRedCell fuzhiDicHeight:dic];
        }
    }else if (indexPath.section == 2){
        return 200;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
- (void)huifuButtonAction:(UIButton *)btn{
    CGPoint point = btn.center;//找到btn中心点记录下来
    point = [self.tableView convertPoint:point fromView:btn.superview];
    NSIndexPath* indexpath = [self.tableView indexPathForRowAtPoint:point];
    ZTXRedBottomCell *cell = [self.tableView cellForRowAtIndexPath:indexpath];
    [self huifuYiChang:cell];
  
}
- (void)huifuYiChang:(ZTXRedBottomCell *)cell{
    if (self.contentStr.length == 0) {
        [XSInfoView showInfo:@"请填输入回复内容" onView:self.view];
        return;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:self.code forKey:@"code"];
    [params setValue:self.code forKey:@"parentCode"];
    [params setValue:self.contentStr forKey:@"handleIdea"];

    NSString *urlString = CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], FEEDBACKHANDLE);
    
    [HTTPManager postNetworkRequest: urlString withParameter:params withVC:self netName:self.navigationItem.title success:^(id response, NSString *msg) {
        cell.txtView.text = @"";
        [self confirmButtonClick];
        NSLog(@"%@",response);
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}



@end
